package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * className: BookServiceImpl
 * description:
 * date: 2023/2/22 0022-PM 11:00:54
 * <p>
 * project: springboot
 * package: com.itheima.service.impl
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Service
public class BookServiceImpl2 implements BookService {

    @Autowired
    private BookDao bookDao;


    /**
     * 保存
     *
     * @param book {@link Book} object
     * @return {@link Boolean}
     */
    @Override
    public Boolean save(Book book) {
        return bookDao.insert(book) > 0;
    }

    /**
     * 更新
     *
     * @param book {@link Book} object
     * @return {@link Boolean}
     */
    @Override
    public Boolean update(Book book) {
        return bookDao.updateById(book) > 0;
    }

    /**
     * 删除
     *
     * @param id {@link Integer} id of the book
     * @return {@link  Boolean}
     */
    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    /**
     * 根据 id 查询
     *
     * @param id {@link Integer} id of the book
     * @return {@link Book} object
     */
    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    /**
     * 获取所有结果
     *
     * @return {@link List <Book>} result
     */
    @Override
    public List<Book> getAll() {
        return bookDao.selectList(null);
    }

    /**
     * 分页查询
     *
     * @param currentPage {@link Integer}
     * @param pageSize    {@link Integer}
     * @return {@link IPage <Book>}
     */
    @Override
    public IPage<Book> getPage(int currentPage, int pageSize) {
        IPage<Book> page = new Page<>(currentPage, pageSize);
        bookDao.selectPage(page, null);
        return page;
    }
}
